package com.tonggedata.dataapi.gateway;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 *
 * @author tonggedata
 * @date 2022-6-26 09:23:23
 */
@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        String apiKey = "edd1c9f034335f136f87ad84b625c8f1";
        List<HeaderRequestInterceptor> interceptors = new ArrayList<>();
        interceptors.add(new HeaderRequestInterceptor(apiKey));

        RestTemplate restTemplate = new RestTemplate(factory);
        restTemplate.setInterceptors(Collections.unmodifiableList(interceptors));
        return restTemplate;
    }

    /**
     * 支持patch方法
     * @return
     */
    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory();
        return factory;
    }

}
